package com.xhji.web.config.lua;

import java.util.function.Supplier;

public class Lua {
    private StringBuilder sb = new StringBuilder();

    public Lua define(String var, String exp) {
        sb.append("local").append(" ").append(var).append("=").append(exp).append("\n");
        return this;
    }

    public Lua condition(String var1, String opt, String var2, Supplier<String> body) {
        sb.append("if(").append(var1).append(opt).append(var2).append(")").append("\n").append("\tthen").append("\n\t\t").append(body.get()).append("\n").append("\tend").append("\n");
        return this;
    }

    public String end(String var) {
        sb.append("return ").append(var);
        return sb.toString();
    }

    public String end() {
        return sb.toString();
    }

    public Lua forEach(String collection, Supplier<String> body) {
        sb.append("for ").append("i,v ").append("in ipairs(").append(collection).append(") do\n").append("\t").append(body.get()).append("\n").append("end\n");
        return this;
    }

    public static Lua function() {
        return new Lua();
    }
}
